package com.abigdreamer.im.client.module.talk.face;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingConstants;

import com.abigdreamer.swing.app.skin.SkinConst;
import com.abigdreamer.swing.component.JCTextPane.ChatPic;

public class PicsJWindow extends JWindow {
	
	private static final long serialVersionUID = 1L;
	
	public static interface PicWindowEvent {
		void onClick(ChatPic image);
	}
	
	private static final int IMG_H_SIZE = 15;
	private static final int IMG_V_SIZE = 7;
	private static final int IMG_SIZE = 28;
	
	private PicWindowEvent windowEvent;
	
	JLabel[] ico = new JLabel[IMG_H_SIZE * IMG_V_SIZE]; /* 放表情 */
	int i;
	
	String[] intro = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
			"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
			"", "", "", "", "", "", "", "", "", "", "", "", "", "", };/* 图片描述 */

	public PicsJWindow(JFrame owner, PicWindowEvent windowEvent) {
		super(owner);
		
		this.windowEvent = windowEvent;
		
		try {
			init();
			this.setAlwaysOnTop(true);
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}

	private void init() throws Exception {
		
		this.setPreferredSize(new Dimension(IMG_SIZE * IMG_H_SIZE, IMG_SIZE * IMG_V_SIZE));
		
		JPanel p = new JPanel();
		p.setOpaque(true);
		p.setLayout(new GridLayout(IMG_V_SIZE, IMG_H_SIZE));
		p.setBackground(Color.WHITE);
		this.setContentPane(p);
		
		String fileName = "";
		for (i = 0; i < ico.length; i++) {
			fileName = "qqdefaultface/" + i + ".gif";/* 修改图片路径 */
			
			String fileUrl = SkinConst.IMAGE_DIR + SkinConst.FILE_SEP + fileName;
			BufferedImage picImg = GifUtil.getFrames(new File(fileUrl))[0].image;
			
			ico[i] = new JLabel(new ChatPic(picImg, i), SwingConstants.CENTER);
			ico[i].setBorder(BorderFactory.createLineBorder(new Color(223, 230, 246), 1));
			ico[i].setToolTipText(i + "");
			ico[i].addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					if (e.getButton() == 1) {
						JLabel cubl = (JLabel) e.getSource();
						ChatPic cupic = (ChatPic) cubl.getIcon();
						
						windowEvent.onClick(new ChatPic(SkinConst.IMAGE_DIR + SkinConst.FILE_SEP +  "qqdefaultface/" + cupic.getIm() + ".gif", cupic.getIm()));
						
						cubl.setBorder(BorderFactory.createLineBorder(new Color(225, 225, 225), 1));
						PicsJWindow.this.dispose();
					}
				}

				@Override
				public void mouseEntered(MouseEvent e) {
					((JLabel) e.getSource()).setBorder(BorderFactory.createLineBorder(Color.BLUE));
				}

				@Override
				public void mouseExited(MouseEvent e) {
					((JLabel) e.getSource()).setBorder(BorderFactory.createLineBorder(new Color(225, 225, 225), 1));
				}

			});
			p.add(ico[i]);
		}
		p.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseExited(MouseEvent e) {
				PicsJWindow.this.dispose();
			}

		});
	}

}
